package com.haochang.chunkvod.controller.activity.login;

import java.util.HashMap;

import org.json.JSONObject;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseEditText;
import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.VodConstant;
import com.haochang.chunkvod.app.common.intent.IntentCode;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.common.user.UserToken;
import com.haochang.chunkvod.app.config.ApiConfig;
import com.haochang.chunkvod.app.config.ServerConfig;
import com.haochang.chunkvod.app.utils.HelperUtils;
import com.haochang.chunkvod.app.utils.SoftKeyboardUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.controller.activity.VodActivity;
import com.haochang.chunkvod.controller.activity.welcome.ChooseCityAndShopActivity;
import com.haochang.chunkvod.model.hostess.IpInfo;
import com.haochang.chunkvod.tools.thirdparty.network.http.httpenum.HttpMethodEnum;
import com.haochang.chunkvod.tools.thirdparty.network.http.httpenum.HttpRequestLoadingEnum;
import com.haochang.chunkvod.tools.thirdparty.network.http.request.HttpRequestBuilder;
import com.haochang.chunkvod.tools.thirdparty.network.http.request.HttpRequestTask;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

/**
 *@author qintao
 *created at 2016/6/3 12:42
 * 登录
 */
public class LoginActivity extends BaseActivity
{

	private BaseEditText tv_account;
	private BaseEditText tv_password;
	private BaseTextView tv_choose_ip;
	private Button btn_login;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
	}

	@Override
	public void receiveParam()
	{

	}

	@Override
	public void initData()
	{

	}

	@Override
	public void initViews()
	{
		setContentView(R.layout.activity_login);
		tv_account = (BaseEditText) findViewById(R.id.tv_account);
		String account = HelperUtils.getHelperInstance().getSValue("staff_number", "");
		if (!TextUtils.isEmpty(account))
		{
			tv_account.setText(account);
			tv_account.setSelection(account.length());
		}
		tv_password = (BaseEditText) findViewById(R.id.tv_password);
		LinearLayout linear_choose = (LinearLayout) findViewById(R.id.linear_choose);//选择ip
		tv_choose_ip = (BaseTextView) findViewById(R.id.tv_choose_ip);
		btn_login = (Button) findViewById(R.id.btn_login);
		linear_choose.setOnClickListener(mOnBaseClickListener);
		btn_login.setOnClickListener(mOnBaseClickListener);
		if (!TextUtils.isEmpty(HelperUtils.getHelperInstance().getSValue(VodConstant.IP_CHOOSED_SHOP, ""))
				&& !TextUtils.isEmpty(HelperUtils.getHelperInstance().getSValue(VodConstant.IP_CHOOSED, "")))
		{
			tv_choose_ip.setText(String.valueOf(HelperUtils.getHelperInstance().getSValue(VodConstant.IP_CHOOSED_CITY, "")
					+ HelperUtils.getHelperInstance().getSValue(VodConstant.IP_CHOOSED_SHOP, "")));
			ServerConfig.qATestEnvironmentIp = HelperUtils.getHelperInstance().getSValue(VodConstant.IP_CHOOSED, "");
			ServerConfig.serverIp = ServerConfig.qATestEnvironmentIp;
		}

	}

	private OnBaseClickListener mOnBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.btn_login:
				final String account = tv_account.getText().toString().trim();
				final String password = tv_password.getText().toString().trim();
				if (TextUtils.isEmpty(account))
				{
					ToastUtils.makeText(LoginActivity.this, R.string.error_empty_account, ToastUtils.TOAST_TIME).show();
					return;
				}
				if (TextUtils.isEmpty(password))
				{
					ToastUtils.makeText(LoginActivity.this, R.string.error_empty_password, ToastUtils.TOAST_TIME).show();
					return;
				}
				SoftKeyboardUtils.closeSoftKeyBoard(LoginActivity.this, tv_password);//收起软键盘
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("staff_number", account);
				map.put("password", password);
				HttpRequestTask httpRequestTask = new HttpRequestBuilder(context).interfaceName(ApiConfig.LOGIN).param(map).httpMethodEnum(HttpMethodEnum.POST)
						.httpRequestLoadingEnum(HttpRequestLoadingEnum.HTTP_LOADING_DEFAULT)
						.httpRequestSucessListener(new HttpRequestBuilder.IHttpRequestSucessListener()
						{

							@Override
							public void onResponseSucess(JSONObject jsonObject)
							{
								if (jsonObject != null)
								{
									String token = jsonObject.optString("authorization");
									UserToken.save(token);
									HelperUtils.getHelperInstance().setValue("staff_number", account);
									handler.sendEmptyMessage(SUCCESS);
								}
							}
						}).httpRequestFailedListener(new HttpRequestBuilder.IHttpRequestFailedListener()
						{

							@Override
							public void onError(int errno, String errMsg)
							{
								if (!TextUtils.isEmpty(errMsg))
									ToastUtils.showText(context, errMsg);
							}
						}).build();
				httpRequestTask.execute();
				break;
			case R.id.linear_choose:
				Intent intent = new Intent(LoginActivity.this, ChooseCityAndShopActivity.class);
				startActivityForResult(intent, IntentCode.IP_CHOOSE_CITY);
				animLeftToRight();
				break;
			default:
				break;
			}
		}
	};

	private final int SUCCESS = 1;
	private final int FAIL = 2;
	private Handler handler = new Handler()
	{
		@Override
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
			switch (msg.what)
			{
			case SUCCESS:
				Intent intent = new Intent(LoginActivity.this, VodActivity.class);
				startActivity(intent);
				finish();
				break;
			case FAIL:
				break;
			}
		}
	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK && requestCode == IntentCode.IP_CHOOSE_CITY)
		{
			if (data != null)
			{
				IpInfo ipInfo = data.getParcelableExtra(IntentKey.IP_CHOOSE_SHOP_ITEM);

				if (null != ipInfo)
				{
					tv_choose_ip.setText(String.valueOf(ipInfo.getCity() + ipInfo.getShop()));
					ServerConfig.qATestEnvironmentIp = ipInfo.getIp();
					ServerConfig.serverIp = ServerConfig.qATestEnvironmentIp;
					HelperUtils.getHelperInstance().setValue(VodConstant.IP_CHOOSED_CITY, ipInfo.getCity());
					HelperUtils.getHelperInstance().setValue(VodConstant.IP_CHOOSED_SHOP, ipInfo.getShop());
					HelperUtils.getHelperInstance().setValue(VodConstant.IP_CHOOSED, ipInfo.getIp());
				}
			}
		}
	}
}
